[test-improver] Improve tests for logger common package#3114
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[test-improver] Improve tests for logger common package#3114github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
…LogLine coverage
- Replace t.Errorf/t.Fatal patterns with testify assertions:
- TestInitLogFile_InvalidDirectory: use require.Error instead of if/t.Fatal
- TestInitLogFile_EmptyFileName: use require.Error instead of if/t.Fatal
- TestInitLogger_FileLogger/JSONLLogger/MarkdownLogger: replace t.Errorf
in error-handler callbacks with boolean flag + assert.False
- TestInitLogger_FileLoggerFallback/JSONLLoggerError/MarkdownLoggerFallback:
replace t.Errorf in setup-handler callbacks with boolean flag + assert.False
- TestInitLogger_SetupError: same pattern for error-handler callback
- Add TestFormatLogLine to cover the previously untested formatLogLine function:
- Tests all four log levels (INFO, WARN, ERROR, DEBUG)
- Verifies bracket structure [timestamp] [level] [category] message
- Validates RFC3339 UTC timestamp format and time window
- Tests format string interpolation with args
- Tests empty category edge case
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Improvements:
internal/logger/common_test.goFile Analyzed
internal/logger/common_test.gointernal/loggerImprovements Made
1. Better Testify Patterns — Replace
t.Errorf/t.Fatalwith Idiomatic AssertionsProblem: Several tests used
t.Errorf("should not be called")inside callback closures andt.Fatalinsideif err == nilguards instead of proper testify patterns.TestInitLogFile_InvalidDirectory— replacedif err == nil { file.Close(); t.Fatal(...) }withrequire.Error(err, ...)after closing any file handleTestInitLogFile_EmptyFileName— same pattern; addedrequire := require.New(t)so the test uses testifyrequire.ErrorconsistentlyTestInitLogger_FileLogger— replacedt.Errorf("Error handler should not be called")inside callback with aerrorHandlerCalledboolean flag +assert.False(errorHandlerCalled, ...)TestInitLogger_FileLoggerFallback— replacedt.Errorfin setup callback withsetupHandlerCalledflag +assert.FalseTestInitLogger_JSONLLogger— same improvement as FileLoggerTestInitLogger_JSONLLoggerError— same improvement as FileLoggerFallbackTestInitLogger_MarkdownLogger— same improvement as FileLoggerTestInitLogger_MarkdownLoggerFallback— same improvement as FileLoggerFallbackTestInitLogger_SetupError— replacedt.Errorfin error-handler callback with flag +assert.FalseThe boolean flag pattern makes the intent explicit alongside the other post-call assertions and avoids mixing raw
t.Errorfwith testify assertions in the same test.2. Increased Coverage — Direct Tests for
formatLogLineProblem: The
formatLogLinefunction incommon.gowas exercised only indirectly throughFileLogger.Log()andServerFileLogger, but had no direct unit tests. The function is responsible for the canonical log line format used across all file-based loggers.TestFormatLogLinewith subtests covering:INFO,WARN,ERROR,DEBUG) appear in bracketscount=%d name=%s)^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\] \[INFO\] \[auth\] event occurred$[]bracket pair3. Cleaner & More Stable Tests
"strings"and"time"imports for the newTestFormatLogLinetestt.Errorfwithassert/requirein the same testsrequire.Errorstops the testWhy These Changes?
internal/logger/common_test.gotests the core initialization primitives (closeLogFile,initLogFile,initLogger) used by all logger types in the package. TheformatLogLinefunction — which defines the log line format for all operational logs — was entirely untested directly. Thet.Errorfcallback pattern, while valid Go, was inconsistent with the testify style used throughout the rest of the file.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests